from app import app
from flask import Flask, render_template, request, jsonify, make_response, session
from flask_restful import inputs
from app.libs.Codes import TextCode
from app.libs.API_used import API_query_addr
from app.libs.Verifys import passWord
from app.models import *
from app.admin.admin_login import admin_require
from app.libs.QINIU import delect as QiniuDel, pfop_m3u8
from sqlalchemy import or_
import time
import hashlib
import random
from app.setting import *
from urllib import parse
from html import unescape
import re
from sqlalchemy.orm.attributes import flag_modified



#获取轮播图
@app.route('/pdd/pager/view', methods=["get"])
def pager_view():
    zone = request.args.get("type",type=int)

    pager = ViewPager.query.filter(ViewPager.zone==zone,ViewPager.status==True).order_by(ViewPager.rank.desc()).limit(8).all()
    print(pager)
    data = [{
        "name":i.name,
        "href":i.href,
        "imgName":i.imgName,
    }for i in pager]

    return jsonify(code=0,msg="获取成功",data=data)


# 获取功能视图
@app.route('/pdd/func/view', methods=["get"])
def func_view():

    pager = FuncData.query.filter(FuncData.status==True).order_by(FuncData.rank.desc()).limit(8).all()
    data = [{
        "name": i.name,
        "href": i.href,
        "imgName": i.imgName,
        "intro":i.intro,
        "useCount":i.useCount,
    } for i in pager]

    return jsonify(code=0, msg="获取成功", data=data)


# 获取热门文章
@app.route('/pdd/article/view', methods=["get"])
def article_view():

    pager = Article.query.filter(Article.flag==1).order_by(Article.read.desc(),Article.Istop.desc()).limit(8).all()
    data = []

    page = []
    for i in pager:
        page.append({
            "id": i.id,
            "title": i.title,
            "read": i.read,
            "cover": i.cover,
            "intro": i.intro,
            "releaseTime":i.releaseTime.strftime('%Y-%m-%d'),
        })
        if len(page) == 4:
            data.append(page)
            page = []

    data.append(page)
    return jsonify(code=0, msg="获取成功", data=data)

# 获取学习视频
@app.route('/pdd/video/view', methods=["get"])
def video_view():

    pager = Video.query.filter(Video.flag==1).order_by(Video.createtime.desc(),Video.Istop.desc()).limit(8).all()
    data = [{
        "id":i.id,
        "title": i.title,
        "read": i.read,
        "cover": i.cover,
        "intro":i.intro,
        "duration":round(i.duration,2),
    } for i in pager]

    return jsonify(code=0, msg="获取成功", data=data)

# 获取学习视频榜单
@app.route('/pdd/video/rank', methods=["get"])
def video_rank():

    pager = Video.query.filter(Video.flag==1).order_by(Video.read.desc()).limit(8).all()
    data = [{
        "id":i.id,
        "title": i.title,
        "read": i.read,
        "cover": i.cover,
        "intro":i.intro,
        "duration":round(i.duration),
    } for i in pager]

    return jsonify(code=0, msg="获取成功", data=data)

# 获取学习视频榜单
@app.route('/pdd/footer', methods=["get"])
def footer():
    ICP = Config.query.filter(Config.key=="ICP").first()
    if not ICP:
        return jsonify(code=-1,msg="请求出错～")
    Fser = Config.query.filter(Config.key=="Fser").first()
    if not Fser:
        return jsonify(code=-1, msg="请求出错～")
    data = {
        "ICP":ICP.value,
        "Fser":Fser.value,
    }

    return jsonify(code=0, msg="获取成功", data=data)
